package edu.ncsu.csc.ip.routing.ls;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;

/**
 * Class <code>link_state</code>
 * 
 *  Runs LinkState to comply with project 3 description 
 * 
 * @author Moin Ayazifar <mayazif@ncsu.edu>
 * @author Steven Elliott <sdelliot@ncsu.edu>
 * 
 */

public class all_shortest_paths {

	public static Comparator<Node> NodeNameComparator = new Comparator<Node>() {
		public int compare(Node node1, Node node2) {
			// ascending order
			return Integer.valueOf(node1.getName()).compareTo(
					Integer.valueOf(node2.getName()));
		}
	};

	public static void main(String[] args) throws IOException {
		try {
			LinkState linkState = new LinkState();
			linkState.loadGraph(args[0]);
			printResults(args[1], args[2], linkState);
			//printResults(args[2], args[1], linkState);
			
			AllPaths allPaths = new AllPaths();
			allPaths.loadGraph(args[0]);
			int shortestPathHops = (linkState.leastCostPathTo(linkState.nodesMap.get(args[2])).size()-1);
			allPaths.findAllPaths(new ArrayList<Node>(),
					allPaths.nodesMap.get(args[1]),
										allPaths.nodesMap.get(args[2]), shortestPathHops+1);
			System.out.println("Path Count: " + allPaths.pathCount);
			
			
		} catch (Exception e) {
			System.err
					.println("USAGE:\n java link_state <file name> <source node> <dest node>\n Example:\njava link_state ./network/node1.txt 1 10");
			e.printStackTrace();
		}
		
		
		
	}

	public static void printResults(String sourceString,
			String destinationString, LinkState linkState) {
		linkState.initialize();
		Node source = linkState.nodesMap.get(sourceString);
		Node destination = linkState.nodesMap.get(destinationString);
		linkState.populateLeastCostPaths(source);

		System.out.println("Source: " + source);
		System.out.println("Destination: " + destination);
		System.out.println("Least Cost: " + destination.getMinCost());
		System.out.println("Hops Count: " + (linkState.leastCostPathTo(destination).size()-1));
		System.out.println("Path: " + linkState.leastCostPathTo(destination));
		
		System.out.println("------------------------------------------\n\n");
	}
}
